/*
这个js文件是进行用户的登录、注册、找回密码的操作
@time: 2019/1/22
@author: cjn
* */

// 页面加载之后的一些操作
$(document).ready(function () {
    var regist_bt = $("#regist_bt");
    regist_bt.attr("disabled", "true");
    var regist_email = $("#regist_email");
    var regist_user = $("#regist_user");
    var regist_password = $("#regist_password");
    var rept_regist_password = $("#rept_regist_password");
    var regist_watch_info = $("#regist_watch_info");
    var find_password_email = $("#find_password_email");

    // 初始化ajax csrf_token
    $.ajaxSetup({
        headers: {"X-CSRFToken": csrf_token}
    });

    //注册邮箱检测，不能为空
    regist_email.blur(function () {
        if (regist_email.val() === "") {
            regist_bt.attr("disabled", "true");
            regist_watch_info.empty();
            console.log(regist_email.attr("placeholder"));
            regist_watch_info.append(
                "<font color='#FF0000'>" +
                regist_email.attr("placeholder") +
                "不能为空</font>"
            );
        } else {
            regist_bt.removeAttr("disabled");
        }
    });

    // 检测用户名，不能为空
    regist_user.blur(function () {
        if (regist_user.val() === "") {
            regist_bt.attr("disabled", "true");
            regist_watch_info.empty();
            console.log(regist_user.attr("placeholder"));
            regist_watch_info.append(
                "<font color='#FF0000'>" +
                regist_user.attr("placeholder") +
                "不能为空</font>"
            );
        } else {
            regist_bt.removeAttr("disabled");
        }
    });

    // 检测密码，不能为空
    regist_password.blur(function () {
        if (regist_password.val() === "") {
            regist_bt.attr("disabled", "true");
            regist_watch_info.empty();
            console.log(regist_password.attr("placeholder"));
            regist_watch_info.append(
                "<font color='#FF0000'>" +
                regist_password.attr("placeholder") +
                "不能为空</font>"
            );
        } else {
            judge_password();
        }
    });

    // 检测重复密码，不能为空
    rept_regist_password.blur(function () {
        if (rept_regist_password.val() === "") {
            regist_bt.attr("disabled", "true");
            regist_watch_info.empty();
            console.log(rept_regist_password.attr("placeholder"));
            regist_watch_info.append(
                "<font color='#FF0000'>" +
                rept_regist_password.attr("placeholder") +
                "不能为空</font>"
            );
        } else {
            judge_password();
        }
    });

    // 检测找回密码的邮箱
    find_password_email.blur(function () {
        var find_btn = $("#find_btn");
        var email_fail = $("#email-fail");
        if (find_password_email.val() === "") {
           find_btn.attr("disabled", "true");
           email_fail.empty();
           email_fail.append("<font color='#FF0000'>" +
               find_password_email.attr("placeholder") +
               "不能为空</font>"
           );
        } else {
            judge_email(find_password_email.val());
        }
    });

    // 检测两次输入的密码是否一致
    function judge_password() {
        var re_ps_val = regist_password.val();
        var rept_re_ps_val = rept_regist_password.val();
        if (
            re_ps_val !== rept_re_ps_val &&
            re_ps_val !== "" &&
            rept_re_ps_val !== ""
        ) {
            regist_bt.attr("disabled", "true");
            regist_watch_info.empty();
            console.log(rept_regist_password.attr("placeholder"));
            regist_watch_info.append(
                "<font color='#FF0000'>两次输入的密码不一致</font>"
            );
        } else {
            regist_watch_info.empty();
            regist_bt.removeAttr("disabled");
        }
    }

    function judge_email(email) {
        var reg = new RegExp("^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$");
        if (!reg.test(email)) {
            var find_btn = $("#find_btn");
            var email_fail = $("#email-fail");
            find_btn.attr("disabled", "true");
            email_fail.empty();
            email_fail.append("<font color='#FF0000'>" +
                find_password_email.attr("placeholder") +
                "邮箱格式不对</font>"
            );
        }
    }
});

// 注册功能
function regist_func() {
    var form_register = $(".form-register");
    console.log("register");
    $.ajax({
        type: "post",
        url: "/users/register/",
        dataType: "json",
        data: form_register.serialize(),
        success: function (ret_data) {
            if (ret_data["status"] === 1) {
                alert("注册的验证信息已经发送至您的邮箱，请您点击确认");
                window.location.href = "/users/login";
            } else {
                var regist_bt = $("#regist_bt");
                regist_bt.attr("disabled", "true");
                var regist_watch_info = $("#regist_watch_info");
                regist_watch_info.empty();
                // console.log(rept_regist_password.attr("placeholder"));
                regist_watch_info.append(
                    "<font color='#FF0000'>" + ret_data["error"] + "</font>"
                );
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            // alert("chen");
            console.log(errorThrown)
        }
    });
}

// 登录功能
function login() {
    var form_login = $(".form-login");
    console.log("login");
    // var login_user = $("#login_user").val();
    // var login_password = $("#login_password").val();
    $.ajax({
        type: "POST",
        url: "/users/login/",
        async: true,
        data: form_login.serialize(),
        success: function (ret_data) {
            console.log("success");
            if (ret_data["status"] === 1) {
                window.location.href = "/index/";  // 返回到主页
            } else {
                var login_faild = $("#login_faild");
                login_faild.empty();
                login_faild.append(ret_data["error"]);
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert("chen");
            console.log(errorThrown)
        }
    });
}

// 找回密码功能
function find_pwd() {
    var form_findpwd = $(".form-findpwd");

    $.ajax({
        type: "POST",
        url: "/users/forgetpwd/",
        async: true,
        data: form_findpwd.serialize(),
        success: function (ret_data) {
            if (ret_data["status"] === 1) {
                alert("您的密码已经进行了重置，重置密码已经发送到您的邮箱，请您登录后立刻进行修改密码！");
                window.location.href = "/users/login/";
            } else {
                var find_fail = $("#email-fail");
                find_fail.empty();
                find_fail.append(ret_data["error"]);
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            // alert("chen");
            console.log(errorThrown)
        }
    })
}